#!/usr/bin/env python3
# -*-coding : utf-8 -*-
# @time     : 2019/9/22 0022 下午 5:28
# @Author   : Administrator
# @FileName : 爬取任意城市对应的肯德基餐厅的位置信息.py
# --------------------------------------------------------

"""
爬取任意城市对应的肯德基的餐厅位置信息
"""
import json
import requests

# 1.指定url
url = "http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword"
city = input("Enter a city:")
# 封装数据
data = {
    "cname": "",
    "pid": "",
    "keyword": city,
    "pageIndex": 1,
    "pageSize": 10
}
# UA伪装
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0"
}
# 2.发起请求
response = requests.post(url=url, data=data, headers=headers)
# 编码方式为utf8
response.encoding = "utf8"
# 3.获取响应数据
json_text = response.text
json_obj = json.loads(json_text)
# print(json_text)
print(json_obj)
# 总行数
row_count = json_obj['Table'][0]['rowcount']
print("总行数:", row_count)
page_count = len(json_obj['Table1'])
print("每页的行数:", page_count)
index = 10
page_num = 2
while index < row_count:
    if index == row_count:
        break
    data = {
        "cname": "",
        "pid": "",
        "keyword": city,
        "pageIndex": page_num,
        "pageSize": 10
    }
    response = requests.post(url=url, data=data, headers=headers)
    response.encoding = "utf8"
    json_text = response.text
    json_obj = json.loads(json_text)
    print(json_obj)
    page_count = len(json_obj['Table1'])
    index += page_count
    page_num += 1
